let mysql = require('mysql2');

function MysqlClient(options = {}){
    let host = options.host || '127.0.0.1';
    let port = options.port || 3306;
    let database = options.database;
    let userName = options.userName;
    let password = options.password;
    let url = options.url || `mysql://${userName}:${password}@${host}:${port}/${database}`;

    let ctx = {
        url: url,
        name: database,
        isOpen: false,
        open: function(){
            return new Promise(function (resolve, reject){
                if (!ctx.isOpen) {
                    let conn = mysql.createConnection({
                        host: host,
                        port: port,
                        user: userName,
                        password: password,
                        database: database
                    });
                    ctx.conn = conn;
                    ctx.isOpen = true;
                }
                resolve();
            });
        },
        close: function(){
            if (ctx.isOpen) {
                ctx.isOpen = false;
                ctx.conn.end();
            }
        },
        find: function(sql, params){
            return new Promise(function (resolve, reject){
                ctx.conn.query(sql, params, function (error, results, fields) {
                    if (error){
                        reject(error);
                    }else{
                        resolve(results);
                    }
                });
            });
        },
        findOne: function(sql, params){
            return new Promise(function (resolve, reject){
                ctx.conn.query(sql, params, function (error, results, fields) {
                    if (error){
                        reject(error);
                    }else{
                        if(results.length == 0){
                            resolve(null);
                        }else{
                            resolve(results[0]);
                        }
                    }
                });
            });
        },
        execute: function(sql, params){
            return new Promise(function (resolve, reject){
                ctx.conn.query(sql, params, function (error, results, fields) {
                    if (error){
                        reject(error);
                    }else{
                        resolve(results);
                    }
                });
            });
        },
        beginTransaction: function() {
            return new Promise(function (resolve, reject){
                ctx.conn.beginTransaction(function (error) {
                    if (error){
                        reject(error);
                    }else{
                        resolve();
                    }
                });
            });
        },
        commit: function () {
            return new Promise(function (resolve, reject){
                ctx.conn.commit(function (error) {
                    if (error){
                        reject(error);
                    }else{
                        resolve();
                    }
                });
            });
        },
        rollback: function () {
            return new Promise(function (resolve, reject){
                ctx.conn.rollback(function (error) {
                    if (error){
                        reject(error);
                    }else{
                        resolve();
                    }
                })
            })
        }
    };

    return ctx;
}

module.exports = {
    create: function(options) {
        return new MysqlClient(options);
    }
};
